package com.hippo.javacore.multithreading;

import java.util.concurrent.ExecutionException;

/**
 * @ClassName ThreadDemo04
 * @Description TODO 常见线程方法--获取线程对象、名字、睡眠
 * @Author tangxl
 * @create 2023-03-13 08:36
 **/
public class ThreadDemo4 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 创建一个MyCallable类实现Callable接口,重写call(有返回值,表示多线程运行结果)
        Runnable callable = ()->{
            System.out.println(Thread.currentThread().getName() + "执行结果" + ((int) Math.random() * 100) + 1);
        };

        // 创建线程
        Thread t1 = new Thread(callable);
        Thread t2 = new Thread(callable);

        // 线程设置名称
        t2.setName("线程2");

        // 启动线程
        t1.start();
        // 线程睡眠
        Thread.sleep(2000);
        t2.start();

        // 获取当前正在执行的线程
        String ThreadName = Thread.currentThread().getName();
        System.out.println("当前线程名称：" + ThreadName);

        System.out.println("t1线程名称：" + t1.getName());
        System.out.println("t2线程名称：" + t2.getName());
    }




}
